package com.ruan.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruan.common.Code;
import com.ruan.common.Result;
import com.ruan.mapper.TasksMapper;
import com.ruan.pojo.Employee;
import com.ruan.pojo.Tasks;
import com.ruan.pojoExt.TaskExt;
import com.ruan.service.ProjectService;
import com.ruan.service.TasksService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service//交由spring容器进行管理//  mybatis-plus提供的规范，继承父类，实现父接口
public class TasksServiceImpl extends ServiceImpl<TasksMapper, Tasks> implements TasksService {

    @Autowired
    TasksMapper tasksMapper;

    @Autowired
    ProjectService projectService;

    @Override
    public Boolean approvalTask(Integer id) {
        LambdaQueryWrapper<Tasks> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Tasks::getId,id);
        Tasks tasks = tasksMapper.selectOne(queryWrapper);
        tasks.setCompletionStatus(Tasks.CompletionStatus.completed);
//        System.out.println("tasks现在是："+tasks);
        tasksMapper.updateById(tasks);//更新到数据库
        return true;
    }

    @Override
    public List<TaskExt> getTodayTasks(String employeeId) {
        LocalDate currentDate = LocalDate.now();
        LambdaQueryWrapper<Tasks> queryWrapper = new LambdaQueryWrapper<>();
//        System.out.println("employeeId是"+employeeId);
        queryWrapper.eq(Tasks::getEmployeeId, employeeId);

        // 从数据库中获取该员工今天的任务
        queryWrapper.eq(Tasks::getEmployeeId, employeeId)
                .ge(Tasks::getDeadline, currentDate) // 获取截止时间大于等于当前日期的任务
                .ne(Tasks::getCompletionStatus, "completed");// 获取completeStatus不为completed的任务
        List<Tasks> tasksList = tasksMapper.selectList(queryWrapper);
        List<TaskExt> taskExtList = new ArrayList();
        for(Tasks tasks : tasksList){
            TaskExt taskExt = new TaskExt();
            BeanUtils.copyProperties(tasks,taskExt);
            Long projectId = tasks.getProjectId();
            taskExt.setProjectName(projectService.getById(projectId).getProjectName());

            taskExtList.add(taskExt);
        }
//        System.out.println("taskExtList有什么"+taskExtList);
        return taskExtList;
    }
}
